package com.asw.springboot.common;

import com.asw.springboot.common.exception.AdminException;
import com.asw.springboot.common.exception.ApiException;
import com.asw.springboot.common.result.GlobalErrorInfoEnum;
import com.asw.springboot.common.result.ResultBody;
import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by hxq  on 2017/5/15.e
 */
public class CommonController{

    protected final static Logger logger = Logger.getLogger(CommonController.class);

    protected HttpServletRequest request;
    protected HttpServletResponse response;
    public String currentVersion;


    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        this.request = request;
        this.response = response;
    }

    @ExceptionHandler(value = ApiException.class)
    @ResponseBody
    public ResultBody exp(Exception ex) {
        // 记录日志
        logger.error("调用API接口出现异常：" + request.getRequestURL() +" 请求方式:"+ request.getMethod()+"  "+getRequestAsg(request)
                +",版本号：" + getCurrentVersion() + ",客户端："
                + getCurrentClientType() + ",设备：" + getCurrentDeviceType()+
                ",错误信息为:"+ex.toString());
        return new ResultBody(GlobalErrorInfoEnum.SYS_ERROR);
    }

    @ExceptionHandler(value = AdminException.class)
    public String procAdminError(Exception ex) {
        // 记录日志
        logger.error("调用API接口出现异常：" + request.getRequestURL() +" 请求方式:"+ request.getMethod()+"  "+getRequestAsg(request)
                +",版本号：" + getCurrentVersion() + ",客户端："
                + getCurrentClientType() + ",设备：" + getCurrentDeviceType()+
                ",错误信息为:"+ex.toString());
        return "error500";
    }





    /***
     * 得到 request请求参数
     * @param request
     * @return
     */
    public String getRequestAsg(HttpServletRequest request){
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()){
            sb.append("参数名:").append(entry.getKey()).append("-参数值:").append(Arrays.toString(entry.getValue()));
        }
        return sb.toString();
    }


    /**
     * 当前版本号去掉点
     *
     * @return
     */
    public String getCurrentVersion() {
        currentVersion = request.getParameter("currentVersion");
        if (!StringUtils.isEmpty(currentVersion)) {
            currentVersion = currentVersion.trim().replace(".", "");
        }
        return currentVersion;
    }

    public int getPageSize() {
        int pageSize;
        if ("".equals(request.getParameter("pageSize")) || request.getParameter("pageSize") == null) {
            pageSize = 20;// 默认每页显示10
        } else {
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }
        return pageSize;
    }

    public int getPageNum() {
        int pageNum;
        if ("".equals(request.getParameter("pageNum")) || request.getParameter("pageNum") == null) {
            pageNum = 1;
        } else {
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
        }
        return pageNum;
    }

    /**
     * 当前设备
     *
     * @return
     */
    public String getCurrentDeviceType() {
        return request.getParameter("currentDeviceType");
    }

    /**
     * 当前客户端类型
     *
     * @return
     */
    public String getCurrentClientType() {
        return request.getParameter("currentClientType");
    }

}
